package com.cci.kangdao.service.impl;

import com.cci.kangdao.crmapi.CRMHttpClientUtil;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.crmapi.CRMServiceApiImpl;
import com.cci.kangdao.crmapi.CRMServiceConst;
import com.cci.kangdao.service.SRTService;
import com.cci.kangdao.utilTool.ParameterTool;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @description:
 * @author: joe
 * @company: CTY
 * @since: 2023/1/9 16:32
 * @version: V1.0
 **/
@Service
public class SRTServiceImpl implements SRTService {
    @Autowired
    private CRMServiceApiImpl crmServiceApi;

    private Logger log = Logger.getLogger(this.getClass().getName());

    @Override
    public void getSrtAndWfcFromSF(Map<String, Object> map) {
        HttpServletResponse response = (HttpServletResponse) map.get("response");
        map.remove("response");
        Map<String, Object> tokenAndHostMap = crmServiceApi.getCCICrmTokenAndHost();
        // 添加token请求头
        Map<String, Object> headerParameter = crmServiceApi.addCCIEncryptionHeader(tokenAndHostMap);
        // 调取请求
        String channelHost = MapUtils.getString(tokenAndHostMap, "hostUrl", "")
                + CRMServiceConst.GET_SRTANDWFC;
        JSONObject jsonObject = CRMHttpClientUtil.httpPOSTRequest(channelHost, headerParameter, map);

        CRMResponeEntity crmResponeEntity = crmServiceApi.getResponeEntity(jsonObject, "获取成功");
        if (crmResponeEntity.getStatus() != CRMServiceConst.CRM_SYNC_CODE_SUCCESS) {
            // 不处理
            ParameterTool.writeResponse(response, -99, "获取失败", crmResponeEntity.getMessage());
            return;
        } else {
            JSONObject data = crmResponeEntity.getData();
            ParameterTool.writeResponseForNull(response, 0, "获取成功", data);
            return;
        }
    }
}
